package com.sltx.controller;


import com.sltx.controller.base.BaseController;
import com.sltx.response.CommonResult;
import com.sltx.service.TaskService;
import com.sltx.util.ExcelUtil;
import com.sltx.util.PageData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/Task")
public class TaskController extends BaseController {

    @Autowired
    private TaskService taskService;


    //任务界面回显
    @RequestMapping(value = "/getModelAndPackage", method = RequestMethod.GET)
    @ResponseBody
    //标注方法
    @ApiOperation("任务界面回显")
    //对方法参数进行说明
    @ApiImplicitParams({//参数集合
            @ApiImplicitParam(name = "user", value = "用户名", defaultValue = "zhangxu", paramType = "query", required = true, dataType = "String")
    })
    public CommonResult<Map<String, Object>> getModelAndPackage() {
        PageData pd = this.getPageData();
        Map<String, Object> result = taskService.getModelAndPackage(pd);
        return CommonResult.success(result);
    }

    //新建任务
    @RequestMapping("/addTask")
    @ResponseBody
    public String addTask() {
        PageData pd = this.getPageData();
        String result = taskService.addTask(pd);
        return result;
    }

    //获取所有任务
    @RequestMapping("/getTask")
    @ResponseBody
    public Map<String, Object> getTask() {
        Map<String, Object> result = new HashMap<>();
        PageData pd = this.getPageData();
        if (pd.get("tjdata").equals("添加时间")) {
            pd.put("tjdata", "");
        }
        if (pd.get("status").equals("任务状态")) {
            pd.put("status", "");
        }
        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());
        Integer startPage = ((pageSize * (pageNo - 1)));
        pd.put("startPage", startPage.toString());
        result.put("count", taskService.getNum(pd));
        result.put("list", taskService.getTaskList(pd));
        return result;
    }

    //任务详情
    @RequestMapping("/getTaskDetails")
    @ResponseBody
    public Map<String, Object> getTaskDetails() {
        PageData pd = this.getPageData();
        if (pd.get("status").equals("0")) {
            //未完成的任务详情
            Map<String, Object> result = taskService.getTaskDetailsUnfinished(pd);
            return result;
        } else {
            //已完成的任务详情
            Map<String, Object> result = taskService.getTaskDetails(pd);
            return result;
        }
    }

    //任务详情企业列表查询和模糊查询
    @RequestMapping("/getTaskCompaniesList")
    @ResponseBody
    public Map<String, Object> getTaskCompaniesList() {
        PageData pd = this.getPageData();
        return taskService.getTaskCompaniesList(pd);
    }


    //查询股权关系
    @RequestMapping("/getEquityRelationship")
    @ResponseBody
    public Map<String, Object> getEquityRelationship() {
        PageData pd = this.getPageData();
        return taskService.getEquityRelationship(pd);
    }

    //单个企业股权关系下载
    @RequestMapping("/downloadEnterprise")
    @ResponseBody
    public String downloadEnterprise(HttpServletResponse response) {
        PageData pd = this.getPageData();
        // 存储Excel表格列名
        List<String> rowNameList = Arrays.asList(
                "企业名称",
                "统一社会信用代码",
                "关联单位",
                "股权关系一",
                "股权关系一比例",
                "股权关系二",
                "股权关系二比例",
                "股权关系三",
                "股权关系三比例",
                "股权关系四",
                "股权关系四比例",
                "股权关系五",
                "股权关系五比例",
                "股权关系六",
                "股权关系六比例",
                "关联单位级数");
        // 存储Excel表格内容，容器
        List<Map<String, String>> list = new ArrayList<>();
        List<Map<String, Object>> result = taskService.downloadEnterprise(pd);
        if (!CollectionUtils.isEmpty(result)){
        String name = "";
        for (Map m : result) {
            Map<String, String> map = new HashMap<>();
            if (!StringUtils.isEmpty(m.get("entname"))) {
                String entname = m.get("entname").toString();
                name += entname;
                map.put("企业名称", entname);
            }
            if (!StringUtils.isEmpty(m.get("uniscid"))) {
                String uniscid = m.get("uniscid").toString();
                map.put("统一社会信用代码", uniscid);
            }
            if (!StringUtils.isEmpty(m.get("query_c"))) {
                String queryC = m.get("query_c").toString();
                map.put("关联单位", queryC);
            }
            if (!StringUtils.isEmpty(m.get("ent_onp"))) {
                String entonp = m.get("ent_onp").toString();
                map.put("股权关系一", entonp);
            }
            if (!StringUtils.isEmpty(m.get("zb_onp"))) {
                String zbonp = m.get("zb_onp").toString();
                map.put("股权关系一比例", zbonp);
            }
            if (!StringUtils.isEmpty(m.get("ent_twp"))) {
                String enttwp = m.get("ent_twp").toString();
                map.put("股权关系二", enttwp);
            }
            if (!StringUtils.isEmpty(m.get("zb_twp"))) {
                String zbtwp = m.get("zb_twp").toString();
                map.put("股权关系二比例", zbtwp);
            }
            if (!StringUtils.isEmpty(m.get("ent_thp"))) {
                String entthp = m.get("ent_thp").toString();
                map.put("股权关系三", entthp);
            }
            if (!StringUtils.isEmpty(m.get("zb_thp"))) {
                String zbthp = m.get("zb_thp").toString();
                map.put("股权关系三比例", zbthp);
            }
            if (!StringUtils.isEmpty(m.get("ent_fop"))) {
                String entfop = m.get("ent_fop").toString();
                map.put("股权关系四", entfop);
            }
            if (!StringUtils.isEmpty(m.get("zb_fop"))) {
                String zbfop = m.get("zb_fop").toString();
                map.put("股权关系四比例", zbfop);
            }
            if (!StringUtils.isEmpty(m.get("ent_fip"))) {
                String entfip = m.get("ent_fip").toString();
                map.put("股权关系五", entfip);
            }
            if (!StringUtils.isEmpty(m.get("zb_fip"))) {
                String zbfip = m.get("zb_fip").toString();
                map.put("股权关系五比例", zbfip);
            }
            if (!StringUtils.isEmpty(m.get("ent_sip"))) {
                String entsip = m.get("ent_sip").toString();
                map.put("股权关系六", entsip);
            }
            if (!StringUtils.isEmpty(m.get("zb_sip"))) {
                String zbsip = m.get("zb_sip").toString();
                map.put("股权关系六比例", zbsip);
            }
            if (!StringUtils.isEmpty(m.get("rl_num"))) {
                String rlnum = m.get("rl_num").toString();
                map.put("关联单位级数", rlnum);
            }
            list.add(map);

        }
            ExcelUtil.excelPort(name + "股权关系链", rowNameList, list, response);
            return "下载成功";
        }
            return "下载失败";
    }

    //下载企业信息表
    @GetMapping("/FirmExcel")
    @ResponseBody
    public String DownLoadFirmExcel(HttpServletResponse response) {
        //根据任务编号下载文件
        PageData pd = this.getPageData();
        // 存储Excel表格列名
        List<String> rowNameList = Arrays.asList(
                "企业名称",
                "统一社会信用代码",
                "关联单位",
                "行业大类",
                "工商注册时间");
        // 存储Excel表格内容，容器
        List<Map<String, String>> list = new ArrayList<>();
        String name = taskService.getName(pd);
        List<Map<String, Object>> result = taskService.DownLoad(pd);
        if (!CollectionUtils.isEmpty(result)) {
            for (Map m : result) {
                Map<String, String> map = new HashMap<>();
                String entname = m.get("entname").toString();
                String uniscid = m.get("uniscid").toString();
                String query_c = m.get("query_c").toString();
                String industryco_d = m.get("industryco_d").toString();
                String esdate = m.get("esdate").toString();
                map.put("企业名称", entname);
                map.put("统一社会信用代码", uniscid);
                map.put("关联单位", query_c);
                map.put("行业大类", industryco_d);
                map.put("工商注册时间", esdate);
                list.add(map);
            }
            ExcelUtil.excelPort(name, rowNameList, list, response);
            return "下载成功";
        } else {
            return "下载失败";
        }

    }

    //修改暂停状态 0=>00  1==>11(0：已上传，1：审核中，2：匹配中，3：已完成)
    @RequestMapping("/suspend")
    @ResponseBody
    public String Suspend() {
        PageData pd = this.getPageData();
        try {
            taskService.UpdateSuspend(pd);
            return "OK";
        } catch (Exception e) {
            return "服务器繁忙";
        }
    }

    //删除股权关系
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteGQGX() {
        PageData pd = this.getPageData();
        String result = taskService.deleteGQGX(pd);
        return result;
    }


    //批量下载
    @RequestMapping("BatchDownload2")
    @ResponseBody
    public String BatchDownload2(HttpServletResponse response) {
        PageData pd = this.getPageData();
        //根据id下载
        String id = (String) pd.get("id");
        String[] split = id.split(",");
        List<Map> arrayList = new ArrayList<>();
        String nameId = split[0];
        String taskNum = taskService.getName1(nameId);
        pd.put("taskNum", taskNum);
        String name = taskService.getName(pd);
        for (String s : split) {
            int s1 = Integer.parseInt(s);
            pd.put("id", s1);
            Map<String,Object> result = taskService.BatchDownload2(pd);
            arrayList.add(result);
        }
        // 存储Excel表格列名
        List<String> rowNameList = Arrays.asList(
                "企业名称",
                "统一社会信用代码",
                "关联单位",
                "行业大类",
                "工商注册时间");
        // 存储Excel表格内容，容器
        List<Map<String, String>> list = new ArrayList<>();
        for (Map m:arrayList){
            Map<String, String> map = new HashMap<>();
            String entname = m.get("entname").toString();
            String uniscid = m.get("uniscid").toString();
            String query_c = m.get("query_c").toString();
            String industryco_d = m.get("industryco_d").toString();
            String esdate = m.get("esdate").toString();
            map.put("企业名称", entname);
            map.put("统一社会信用代码", uniscid);
            map.put("关联单位", query_c);
            map.put("行业大类", industryco_d);
            map.put("工商注册时间", esdate);
            list.add(map);
        }
        try {
            ExcelUtil.excelPort(name,rowNameList, list, response);
            return "下载成功";
        }catch (Exception e){
            return "下载失败";
        }
    }
}
